<template>
  <div>
    <q-form class="dialog_card column" ref="editRef">
      <div class="row q-col-gutter-x-md dialog_form q-pa-md">
        <div class="col-12">
          <h5><q-icon name="star" color="red" />类型名称：</h5>
          <q-input
            lazy-rules
            :rules="[(val) => (val && val.length > 0) || '请输入类型名称']"
            outlined
            dense
            v-model="isform.type_name"
            type="text"
            clearable
            style="width: 50%"
          />
        </div>

        <div class="col-12">
          <h5>启用状态：</h5>
          <q-toggle
            :false-value="0"
            :true-value="1"
            color="bule"
            v-model="isform.type_state"
          />
        </div>
      </div>
    </q-form>
  </div>
</template>

<script lang="ts" setup>
import emitter from '@/components/eventbus';
import { typeAdd, typeEdit } from '@/api/bbs';
import { useQuasar } from 'quasar';

interface Props {
  form: {
    id?: number;
    type_name?: string; //类型名称
    type_state?: number; //版本状态 0：禁用 1：可用
  };
}
const props = withDefaults(defineProps<Props>(), {
  form: () => ({}),
});
const editRef = ref();
const $q = useQuasar();

//接收兄弟组件传递事件
emitter.on('saveData', async () => {
  console.log('saveDatasaveData');
  IsValidate();
});
//表单验证
const IsValidate = () => {
  if (editRef.value) {
    editRef.value.validate().then(async function (validate: any) {
      if (validate) {
        console.log('验证通过');
        onSubmit();
      } else {
        console.log('验证不通过');
      }
    });
  }
};
//提交数据
const onSubmit = async () => {
  if (isform.value.id) {
    const res: any = await typeEdit(isform.value);
    info(res);
    console.log('编辑111', res);
  } else {
    const res: any = await typeAdd(isform.value);
    info(res);
    console.log('新建111', res);
  }
};
//信息返回
const info = (res: any) => {
  if (res.code == 200) {
    $q.notify({
      color: 'white',
      textColor: 'positive',
      icon: 'check_circle',
      position: 'top',
      message: res.msg,
    });
    emit('backList');
  }
};

const emit = defineEmits(['update:form', 'backList']);

const isform: any = computed({
  set(value: any) {
    // emit('update:form', value);
  },
  get() {
    console.log('props.form', props.form);
    return props.form;
  },
});
</script>
<style scoped lang="scss"></style>
